<template>
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body v-drag-fullscreen>
    <el-row :gutter="10" class="mb8">
      <el-col :span="24">
        <el-form :inline="true" :model="queryParams" ref="queryForm" size="small" label-width="0"
                 class="demo-form-inline" style="height: 40px;float: right;">
          <el-form-item prop="receiptNo">
            <el-input
              v-model="queryParams.businessNo"
              placeholder="业务单号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <!--<el-form-item style="width: 100px;" prop="status">-->
            <!--<el-select v-model="queryParams.status" placeholder="状态" clearable size="small">-->
              <!--<el-option-->
                <!--v-for="dict in dict.advance_receipt_status"-->
                <!--:key="dict.dictValue"-->
                <!--:label="dict.dictLabel"-->
                <!--:value="dict.dictValue"-->
              <!--/>-->
            <!--</el-select>-->
          <!--</el-form-item>-->
          <el-form-item>
            <el-date-picker
              v-model="billTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="预收日期开始"
              end-placeholder="预收日期结束">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"></el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"></el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="advanceReceiptList"
      @selection-change="handleSelectionChange"
      :header-cell-style="styleTitleFunc"
      height="300"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预收单号" align="center" prop="no"/>
      <el-table-column label="预收来源" align="center" prop="businessType">
        <template slot-scope="scope">
          {{ dict.label.business_type[scope.row.businessType] }}
        </template>
      </el-table-column>
      <el-table-column label="业务单号" align="center" prop="businessNo"/>
      <el-table-column label="预收金额" align="center" prop="billMoney"/>
      <el-table-column label="预收日期" align="center" prop="billTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="可退金额" align="center" prop="amountRemaining"/>
      <el-table-column label="退款金额" align="center" prop="drawbackMoney" width="100">
        <template slot-scope="scope">
          <el-input class="special_money_class" @change="(v)=>changeRefundAmount(scope.row,v)" v-model="scope.row.drawbackMoney"></el-input>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-form class="bottomForm" :model="form" ref="form" size="mini" label-position="left" label-width="65px">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="付款人" prop="paymentUserName" >
            <el-input  v-model="form.paymentUserName"  readonly />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="付款日期" prop="paymentTime" >
            <el-date-picker
              v-model="form.paymentTime"
              type="date"
              value-format="yyyy-MM-dd"
              prefix-icon="node"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="退款金额" prop="paymentMoney" >
            <el-input  v-model="form.paymentMoney"  readonly placeholder="请输入收到金额"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="打印模板" prop="printTemplate" >
            <el-select v-model="form.printTemplate" clearable placeholder="请选择打印模板">
              <el-option
                v-for="item in printTempletOptions"
                :key="item.id"
                :label="item.title"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" v-hasPermi="['payfee:advicePayment:add']">退 费</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

    <advice-payment ref="adviceRefs" @ok="createAdviceReceipt"/>

  </el-dialog>
</template>

<script>

  import {listMixin} from "@/utils/mixins/listMixin";
  import {listAdvanceReceipt  } from "@/api/payfee/advanceReceipt";
  import { addAdvicePayment } from "@/api/payfee/advicePayment";
  import { listPrintTemplet } from "@/api/system/printTemplet";
  import AdvicePayment from "./advicePayment";

  export default {
    name: "AdvanceReceipt",
    mixins: [listMixin],
    components:{ AdvicePayment },
    dicts: ['advance_receipt_status', 'business_type'],
    data() {
      return {
        title:"退预收款",
        open:false,
        // 预收单表格数据
        advanceReceiptList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          status: null,
          businessNo: null,
          roomId:null,
          params: {
            statusIN:"10001,10002"
          }
        },
        currentNode: {},
        //预收日期
        billTime: [],
        selection:[],
        printTempletOptions:[],
        form:{}
      };
    },
    methods: {
      show(option) {
        this.reset()
        this.currentNode = option.currentNode
        this.getList();
        this.open = true
        this.getListPrintTemplet()
      },
      /** 查询预收单列表 */
      getList() {
        this.loading = true;
        this.queryParams.roomId = this.currentNode.id || "-1"
        let queryParams = this.addDateRange(this.queryParams, this.billTime, "billTime")
        listAdvanceReceipt(queryParams).then(response => {
          let advanceReceiptList = response.rows
          for(let i in advanceReceiptList){
            advanceReceiptList[i].drawbackMoney = advanceReceiptList[i].amountRemaining
          }
          this.advanceReceiptList = advanceReceiptList;
          this.total = response.total;
          this.loading = false;
        });
      },
      cancel(){
        this.open = false
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.billTime = []
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.madvanceReceiptId)
        this.single = selection.length!==1
        this.multiple = !selection.length
        this.selection = selection
        this.computeOutlay()
      },
      changeRefundAmount(row,v){
        if(v > row.amountRemaining){
          row.drawbackMoney = row.amountRemaining
          this.msgWarning("退款金额不能超出可退款金额")
        }
        this.computeOutlay()
      },
      computeOutlay(){
        let paymentMoney = 0;
        this.advanceReceiptList.forEach((row) => {
          let index = this.selection.findIndex(x=>row.no==x.no)
          if(index != -1){
            paymentMoney = this.$utils.add(paymentMoney,Number(row.drawbackMoney))
          }
        })
        this.form.paymentMoney = paymentMoney
      },
      submitForm(){
        let me = this
        if(!this.selection.length){
          this.msgWarning("请选择需要退款的预收单明细列表")
          return false
        }
        this.$refs.adviceRefs.show({ adviceForm:this.form })
      },
      styleTitleFunc({row,column,rowIndex, columnIndex}){
        if(column.property=='drawbackMoney'){
          return "color: red;"
        }
      },
      //打印模板字典
      getListPrintTemplet(){
        listPrintTemplet({templetType: '10',status:'0'}).then(res=>{
          this.printTempletOptions = res.rows
        })
      },
      reset() {
        this.selection = []
        this.form = {
          paymentMoney:0,
          transferAmount:0,
          paymentTime: this.parseTime(new Date(),'{y}-{m}-{d}'),
          printTemplate: null,
          paymentUserName: this.$store.state.user.nickName,
          modePayments: [
            {
              collectionMethod: '10001',//收款方式
              billMoney: 0//收款金额
            }
          ]
        }
        this.resetForm("form");
      },
      //创建付款单
      createAdviceReceipt(param){
        let params = {
          roomId:this.currentNode.id,
          businessType:'ADVANCE_RECEIPT',
          paymentMoney:param.paymentMoney,
          transferAmount:param.transferAmount,
          modePayments: param.modePayments,
          advanceReceipts:this.selection
        }
        addAdvicePayment(params).then(res=>{
          this.open = false
          this.$emit("ok")
        })
      }

    }
  };
</script>

<style lang="scss" scoped>
  .submit-but{
    text-align: center;
    .link-but{
      margin-left: 10px;
    }
  }

  .operate-but{
    text-align: center;
  }
  ::v-deep .bottomForm{
    margin-top: 20px;
    .el-input__inner{
      border: none;
      border-bottom:1px solid #DCDFE6;
      border-radius: 0;
      padding: 0 5px;
    }
    .el-form-item--mini .el-form-item__label{
      padding: 3px !important;
    }
    .el-input-group__append, .el-input-group__prepend{
      border: none;
      background: none;
      font-weight: bold;
      font-size: 14px;
    }
  }

  ::v-deep .el-table__row{
    .el-input__inner{
      border: none;
      border-bottom: 1px solid #DCDFE6;
      border-radius: 0;
      padding: 0 5px;
    }
  }
  ::v-deep .topForm{
    .el-input__inner{
      border: none;
      border-bottom:1px solid #DCDFE6;
      border-radius: 0;
      padding: 0 5px;
    }
  }
</style>
